package com.itheima.web.controller;

import cn.hutool.captcha.LineCaptcha;
import com.itheima.domain.db.Admin;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.MovementVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.UserInfoAdminVo;
import com.itheima.web.exception.BusinessException;
import com.itheima.web.interceptor.AdminHolder;
import com.itheima.web.manager.AdminManager;
import com.itheima.web.manager.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserManager userManager;
    /*
     *
     *  分页查询用户信息
     * @param page  分页数据
     * @param pagesize 分页数据
     * @author
     * @date
     * @return com.itheima.domain.vo.PageBeanVo
     */

    @GetMapping("/manage/users")
    public PageBeanVo findUsers(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize) throws IOException {

        return userManager.findUsers(page, pagesize);
    }

    /*
     *
     *  根据Id查询用户详细信息
     * @param userId 用户Id
     * @author
     * @date
     * @return com.itheima.domain.db.UserInfo
     */

    @GetMapping("/manage/users/{userId}")
    public UserInfoAdminVo findUserById(
            @PathVariable("userId") Long userId
    )  {
        return userManager.findUserById( userId );
    }
    /**
     * 查询用户的视频数据
     */
    @GetMapping("/manage/videos")
    public PageBeanVo findById(
            Long uid,
            @RequestParam(defaultValue = "1") Integer page ,
            @RequestParam(defaultValue = "10") Integer pagesize
    ) throws IOException {
        return userManager.findVideoByUid(uid , page ,pagesize );
    }
    /**
     * 查询用户的动态数据
     * 此接口还给 审核动态查询使用
     */
    @GetMapping("/manage/messages")
    public PageBeanVo findByMessages(
            Long uid,
            String state,
            @RequestParam(defaultValue = "1") Integer page ,
            @RequestParam(defaultValue = "10") Integer pagesize
    ) throws IOException {
        Integer intState ;
        //前端有坑, 前端没有数据的时候 传入了null 或者是 ''
        if(state == null || "".equals(state) || "''".equals(state)){
            intState  = null;
        }else{
            intState = Integer.valueOf(state);
        }

        return userManager.findByMessages(uid ,intState , page ,pagesize );
    }



    //动态详情
    @GetMapping("/manage/messages/{movementId}")
    public MovementVo findByMessages(
           @PathVariable String movementId
    ) throws IOException {


        return userManager.findMovementById(movementId);
    }
    //评论列表翻页
    @GetMapping("/manage/messages/comments")
    public PageBeanVo findByMessages(
            String messageID, //动态id
           @RequestParam(defaultValue = "1") Integer page ,
           @RequestParam(defaultValue = "10") Integer pagesize
    ) throws IOException {


        return userManager.findCommentByMovementId(page , pagesize,messageID);
    }

    /*
     *
     *  冻结用户
     * @param token
     * @param map
     * @author
     * @date
     * @return java.util.Map
     */

    @PostMapping("/manage/users/freeze")
    public Map userFreeze(@RequestHeader("Authorization") String token,
                          @RequestBody Map restmap
    ){
        Map<String,String> map=new HashMap<>();

        String message =userManager.saveUserFreeze(restmap);

        map.put("message",message);

        return map;
    }





    /*
     *
     *  用户解冻
     * @param restmap   用户Id和解冻原因
     * @author
     * @date
     * @return java.util.Map
     */

    @PostMapping("/manage/users/unfreeze")
    public Map userDeblocking(@RequestBody Map restmap){

        Map<String,String> map=new HashMap<>();

        String message =userManager.saveuserDeblocking(restmap);

        map.put("message",message);

        return map;
    }

}
